﻿using System;
using System.Collections.Generic;
using Core.Log;
using Core.Utils;
using Util;

namespace Core.Event {
    //仅表示事件启动，事件内部不进行数据处理，故不传入参数
    public class GameEventMgr : Singleton<GameEventMgr> {
        private Dictionary<GameEventID, List<Action<GameEvent>>> mEventActionDic;

        protected override void OnCreate() {
            base.OnCreate();
            mEventActionDic = new();
        }

        /// <summary>
        /// 注册事件
        /// </summary>
        public void RegisterEvent(GameEventID eventID, Action<GameEvent> callback) {
            if (!mEventActionDic.ContainsKey(eventID)) {
                mEventActionDic.Add(eventID,new List<Action<GameEvent>>());
            }
            mEventActionDic[eventID].Add(callback);
        }

        /// <summary>
        /// 触发事件
        /// </summary>
        public void PushEvent(GameEventID eventID,params object[] param) {
            if (!mEventActionDic.ContainsKey(eventID)) {
                ADbg.LogEF("[Event] 正在触发没有被注册过的事件，ID：{0}",(int)eventID);
                return;
            }
            GameEvent evt = PoolMgr.Ins.Get<GameEvent>();
            evt.SetEvent(eventID,param);
            for (int i = 0; i < mEventActionDic[eventID].Count; ++i) {
                mEventActionDic[eventID][i]?.Invoke(evt);
            }
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        public void UnRegisterEvent(GameEventID eventID, Action<GameEvent> callback) {
            //获得事件列表
            if (mEventActionDic.ContainsKey(eventID)) {
                mEventActionDic[eventID].Remove(callback);
            }
        }
    }
}